package com.fanqwz.hexieshe.presenter;

import android.content.Context;
import android.util.Log;

import com.fanqwz.hexieshe.R;
import com.fanqwz.hexieshe.dagger.PerActivity;
import com.fanqwz.hexieshe.domain.HeadLine;
import com.fanqwz.hexieshe.domain.usercase.GetHeadLinesUseCase;
import com.fanqwz.hexieshe.mapper.DataMapper;
import com.fanqwz.hexieshe.model.HeadLineModel;
import com.fanqwz.hexieshe.view.HeadlineView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import io.reactivex.observers.DisposableObserver;

/**
 * Created by fanqw on 2017/6/30.
 */

@PerActivity
public class HeadLinePresenter implements Presenter {
    private static int FIRST_PAGE_NUM = 1;
    private static final String TAG = HeadlineObserver.class.getSimpleName();
    private final GetHeadLinesUseCase getHeadLinesUseCase;
    private HeadlineView headlineView;
    private int page = FIRST_PAGE_NUM;
    private String category;
    private String categoryDisplayName;
    private String tag;
    private Map<Integer, List<HeadLineModel>> headLineMap = new HashMap<>();

    @Inject
    Context context;

    @Inject
    public HeadLinePresenter(GetHeadLinesUseCase getHeadLinesUseCase) {
        this.getHeadLinesUseCase = getHeadLinesUseCase;
    }

    public void setView(HeadlineView headlineView) {
        this.headlineView = headlineView;
    }

    public void setCategory(String category) {
        this.category = category;
        page = 0;
    }

    public void setCategoryDisplayName(String categoryDisplayName) {
        this.categoryDisplayName = categoryDisplayName;
    }

    public void setTag(String tag) {
        this.tag = tag;
        page = 0;
    }

    @Override
    public void create() {
    }

    @Override
    public void show() {
        headlineView.prepare();
        doGetHeadline();
    }

    @Override
    public void hide() {
    }

    @Override
    public void destroy() {
    }

    public void refresh() {
        page = FIRST_PAGE_NUM;
        doGetHeadline();
    }

    public void getMore() {
        page++;
        doGetHeadline();
    }

    private void doGetHeadline() {
        GetHeadLinesUseCase.Params params = new GetHeadLinesUseCase.Params(page, category, tag);
        getHeadLinesUseCase.execute(new HeadlineObserver(params), params);

        if (headlineView != null && categoryDisplayName != null)
            headlineView.setTitle(categoryDisplayName);
    }

    class HeadlineObserver extends DisposableObserver<List<HeadLine>> {
        private GetHeadLinesUseCase.Params params;

        public HeadlineObserver(GetHeadLinesUseCase.Params params) {
            this.params = params;
        }

        @Override
        public void onNext(List<HeadLine> value) {
            headLineMap.put(params.page, DataMapper.transformArticles(value));
            Set<Integer> set = headLineMap.keySet();
            List<HeadLineModel> headLineModels = new ArrayList<>();

            for (Integer i : set) {
                headLineModels.addAll(headLineMap.get(i));
            }

            Collections.sort(headLineModels, new Comparator<HeadLineModel>() {
                @Override
                public int compare(HeadLineModel o1, HeadLineModel o2) {
                    return (int) (o2.getId() - o1.getId());
                }
            });
            headlineView.showHeadlines(headLineModels);
            headlineView.setTitle(categoryDisplayName == null ? context.getString(R.string.main_page) : categoryDisplayName);
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, "onError: ", e);
        }

        @Override
        public void onComplete() {
        }
    }
}
